<template>
    <view class="preview" v-if="currentInfo">
        <swiper circular :current="currentIndex" @change="swiperChange">
            <swiper-item v-for="(item,index) in classList" :key="item._id">
                <image v-if="readImg.includes(index)" @click="maskChange" :src="item.picurl" mode=""></image>
            </swiper-item>
        </swiper>
        <view class="mask" v-if="maskState">
            <view class="goBack" @click="goBack" :style="{top:getStatusBarHeight()+'px'}">
                <uni-icons type="back" size="20" color="#fff"></uni-icons>
            </view>
            <view class="count">{{currentIndex+1}} / {{classList.length}}</view>
            <view class="time">
                <uni-dateformat :date="new Date()" format="hh:mm"></uni-dateformat>
            </view>
            <view class="date">
                <uni-dateformat :date="new Date()" format="MM月dd日"></uni-dateformat>
            </view>
            <view class="footer">
                <view class="box" @click="openInfo">
                    <uni-icons type="info" size="25"></uni-icons>
                    <view class="text">信息</view>
                </view>
                <view class="box" @click="openRate">
                    <uni-icons type="star" size="25"></uni-icons>
                    <view class="text">{{currentInfo.score}}分</view>
                </view>
                <view class="box" @click="Download">
                    <uni-icons type="download" size="25"></uni-icons>
                    <view class="text">下载</view>
                </view>
            </view>
        </view>

        <!-- 信息弹窗 -->
        <uni-popup ref="infoPopup" type="bottom">
            <view class="infoPopup">
                <view class="popupHeader">
                    <view></view>
                    <view class="title">壁纸信息</view>
                    <view class="close" @click="infoClose">
                        <uni-icons type="closeempty" size="18" color="#999"></uni-icons>
                    </view>
                </view>
                <scroll-view scroll-y>
                    <view class="content">
                        <view class="row">
                            <view class="lable">壁纸ID：</view>
                            <text selectable class="value">{{currentInfo._id}}</text>
                        </view>
                        <!-- <view class="row">
                            <view class="lable">分类：</view>
                            <text selectable class="value classify">人气美女</text>
                        </view> -->

                        <view class="row">
                            <view class="lable">发布者：</view>
                            <text selectable class="value">{{currentInfo.nickname}}</text>
                        </view>
                        <view class="row">
                            <view class="lable">评分：</view>
                            <view class="value rateBox">
                                <uni-rate v-model="currentInfo.score" readonly="true" touchable="false" />
                                <text class="score">{{currentInfo.score}}分</text>
                            </view>
                        </view>
                        <view class="row">
                            <view class="lable">摘要：</view>
                            <text selectable class="value">{{currentInfo.description}}</text>
                        </view>
                        <view class="row">
                            <view class="lable">标签：</view>
                            <view class="value tabs">
                                <view class="tab" v-for="item in currentInfo.tabs" :key="item">{{item}}</view>
                            </view>
                        </view>
                        <view class="copyright">
                            声明:本图片来用户投稿，非商业使用，用于免费学习交流，如侵犯了您的权益，您可以拷贝壁纸ID举报至平台，邮箱2113007588@qq.com，管理员将删除侵权壁纸，维护您的权益。
                        </view>

                    </view>
                </scroll-view>
            </view>
        </uni-popup>

        <!-- 评分弹窗 -->
        <uni-popup ref="ratePopup" :is-mask-click=false>
            <view class="ratePopup">
                <view class="popupHeader">
                    <view> </view>
                    <view class="title">{{isScore?'已评分':'壁纸评分'}}</view>
                    <view class="close" @click="rateClose">
                        <uni-icons type="closeempty" size="18" color="#999"></uni-icons>
                    </view>
                </view>
                <view class="content">
                    <uni-rate v-model="userScore" allowHalf :disabled="isScore" />
                    <view class="text">{{userScore}}分</view>
                </view>
                <view class="footer">
                    <button size="mini" @click="submitScore" :disabled="!userScore || isScore">确认评分</button>
                </view>
            </view>
        </uni-popup>
    </view>

</template>

<script setup>
    import {
        onLoad
    } from '@dcloudio/uni-app'
    import {
        ref
    } from 'vue'
    import {
        getStatusBarHeight
    } from '../../utils/system.js'
    import {
        apiGetSetupScore,
        apiWriteDownload
    } from '../../api/apis.js'

    const classList = ref([])
    //用户打分
    const userScore = ref(0)
    const isScore = ref(false)

    //遮罩层状态
    const maskState = ref(true)
    const maskChange = () => {
        maskState.value = !maskState.value
    }

    //信息弹窗
    const infoPopup = ref(null)
    const openInfo = () => {
        console.log('信息弹窗');
        infoPopup.value.open();
    }
    const infoClose = () => {
        infoPopup.value.close();

    }

    //评分弹窗
    const ratePopup = ref(null)
    const openRate = () => {
        if (currentInfo.value.userScore) {
            isScore.value = true
            userScore.value = currentInfo.value.userScore
        }
        ratePopup.value.open()
    }
    const rateClose = () => {
        ratePopup.value.close()
        userScore.value = 0
        isScore.value = false
    }


    //返回上一页
    const goBack = () => {
        uni.navigateBack()
    }

    const storgClassList = uni.getStorageSync('storgClassList') || []
    classList.value = storgClassList.map(item => {
        return {
            ...item,
            picurl: item.smallPicurl.replace('_small.webp', '.jpg')
        }
    })
    const currentId = ref(null)
    const currentIndex = ref(0)


    const currentInfo = ref(null)

    onLoad((e) => {
        currentId.value = e.id
        currentIndex.value = classList.value.findIndex(item => item._id == currentId.value)
        readImg.value.push(
            currentIndex.value == 0 ? classList.value.length - 1 : currentIndex.value - 1,
            currentIndex.value,
            currentIndex.value >= classList.value.length - 1 ? 0 : currentIndex.value + 1)
        currentInfo.value = classList.value[currentIndex.value]
        console.log(currentInfo.value);
    })
    const readImg = ref([])
    const swiperChange = (e) => {
        console.log(e);
        currentIndex.value = e.detail.current
        currentInfo.value = classList.value[currentIndex.value]

        readImg.value.push(currentIndex.value == 0 ? classList.value.length - 1 : currentIndex.value - 1,
            currentIndex.value,
            currentIndex.value >= classList.value.length - 1 ? 0 : currentIndex.value + 1)

    }
    readImg.value = [...new Set(readImg.value)]
    const submitScore = async () => {
        const {
            classid,
            _id: wallId
        } = currentInfo.value
        const params = {
            classid,
            wallId,
            userScore: userScore.value
        }
        const res = await apiGetSetupScore(params)
        console.log(res);
        if (res.errCode === 0) {
            uni.showToast({
                title: '评分成功',
                icon: 'none',
            })
            classList.value[currentIndex.value].userScore = userScore.value
            console.log(classList.value);
            uni.setStorageSync("storgClassList", classList.value)
            ratePopup.value.close()
        }

    }

    const Download = async() => {

        // #ifdef H5
        uni.showModal({
            title: '请长按保存壁纸',
            showCancel: false
        })
        // #endif

        // #ifndef H5
        try {

            uni.showLoading({
                title: "下载中...",
                mask: true
            })
            let {
                classid,
                _id: wallId
            } = currentInfo.value;
            let res = await apiWriteDownload({
                classid,
                wallId
            })
            if (res.errCode != 0) throw res;
            uni.getImageInfo({
                src: currentInfo.value.picurl,
                success: (res) => {
                    uni.saveImageToPhotosAlbum({
                        filePath: res.path,
                        success: (res) => {
                            uni.showToast({
                                title: "保存成功，请到相册查看",
                                icon: "none"
                            })
                        },
                        fail: err => {
                            if (err.errMsg == 'saveImageToPhotosAlbum:fail cancel') {
                                uni.showToast({
                                    title: '保存失败，请重新点击下载',
                                    icon: "none"
                                })
                                return;
                            }
                            uni.showModal({
                                title: "授权提示",
                                content: "需要授权保存相册",
                                success: res => {
                                    if (res.confirm) {
                                        uni.openSetting({
                                            success: (setting) => {
                                                console.log(
                                                    setting);
                                                if (setting
                                                    .authSetting[
                                                        'scope.writePhotosAlbum'
                                                    ]) {
                                                    uni.showToast({
                                                        title: "获取授权成功",
                                                        icon: "none"
                                                    })
                                                } else {
                                                    uni.showToast({
                                                        title: "获取权限失败",
                                                        icon: "none"
                                                    })
                                                }
                                            }
                                        })
                                    }
                                }
                            })
                        },
                        complete: () => {
                            uni.hideLoading();
                        }
                    })

                }
            })

        } catch (err) {
            console.log(err);
            uni.hideLoading();
        }
        // #endif
    }
</script>

<style lang="scss" scoped>
    .preview {
        width: 100%;
        height: 100vh;
        position: relative;

        swiper {
            width: 100%;
            height: 100%;

            image {
                width: 100%;
                height: 100%;

            }
        }

        .mask {
            &>view {
                position: absolute;
                left: 0;
                right: 0;
                margin: auto;
                width: fit-content;


            }

            .goBack {
                width: 38px;
                height: 38px;
                background: rgba(0, 0, 0, 0.5);
                left: 30rpx;
                margin-left: 0;
                border-radius: 100px;
                top: 0;
                backdrop-filter: blur(10rpx);
                border: 1rpx solid rgba(255, 255, 255, 0.3);
                display: flex;
                align-items: center;
                justify-content: center;
            }

            .count {
                top: 10vh;
                background: rgba(0, 0, 0, 0.3);
                color: #ffffff;
                border-radius: 40rpx;
                padding: 8rpx 28rpx;
                backdrop-filter: blur(10rpx);
            }

            .time {
                top: calc(10vh + 80rpx);
                color: #fff;
                font-size: 140rpx;
                font-weight: 100;
                line-height: 1em;
                text-shadow: 0 4rpx rgba(0, 0, 0, 0.3);
            }

            .date {
                font-size: 34rpx;
                top: calc(10vh + 230rpx);
                text-shadow: 0 4rpx rgba(0, 0, 0, 0.3);
                color: #fff;

            }

            .footer {
                background: rgba(255, 255, 255, 0.8);
                bottom: 10vh;
                width: 65vw;
                height: 120rpx;
                border-radius: 120rpx;
                display: flex;
                justify-content: space-around;
                align-items: center;
                box-shadow: 0 2rpx 0 rgba(0, 0, 0, 0.1);
                backdrop-filter: blur(20rpx);

                .box {
                    display: flex;
                    flex-direction: column;
                    align-items: center;
                    justify-content: center;
                    padding: 2rpx 10rpx;

                    .text {
                        font-size: 25rpx;
                        color: $text-font-color-2;
                    }

                }
            }

        }

        .infoPopup {

            background: #fff;
            padding: 30rpx;
            border-radius: 30rpx 30rpx 0 0;
            overflow: hidden;

            .popupHeader {
                display: flex;
                justify-content: space-between;
                align-items: center;

                .title {
                    color: $text-font-color-2;
                    font-size: 26rpx;
                }

                .close {
                    padding: 6rpx;
                }
            }

            scroll-view {
                max-height: 60vh;

                .content {
                    .row {
                        display: flex;
                        padding: 16rpx 0;
                        font-size: 32rpx;
                        line-height: 1.7em;

                        .lable {
                            color: $text-font-color-3;
                            width: 140rpx;
                            text-align: right;
                            font-size: 30rpx
                        }

                        .value {
                            flex: 1;
                            width: 0;
                        }

                        .rateBox {
                            display: flex;
                            align-items: center;

                            .score {
                                padding: 0 20rpx;
                                font-size: 30rpx;
                                color: #ffca3e;
                            }
                        }

                        .tabs {
                            display: flex;
                            flex-wrap: wrap;

                            .tab {
                                border: 1px solid $brand-theme-color;
                                color: $brand-theme-color;
                                font-size: 22rpx;
                                padding: 10rpx 30rpx;
                                border-radius: 40rpx;
                                line-height: 1em;
                                margin: 0 10rpx 10rpx 0;
                            }
                        }

                        .classify {
                            color: $brand-theme-color;
                        }

                    }

                    .copyright {
                        margin-top: 20rpx;
                        background: #e6e6e6;
                        padding: 30rpx;
                        border-radius: 20rpx;
                        color: #666;
                        line-height: 1.6em;
                        font-size: 28rpx
                    }
                }
            }
        }

        .ratePopup {
            background: #fff;
            width: 70vw;
            height: 20vh;
            border-radius: 20rpx;

            .popupHeader {
                display: flex;
                align-items: center;
                justify-content: space-between;
                padding: 20rpx 30rpx;

                .title {
                    color: $text-font-color-3;
                }

                .close {
                    padding: 5rpx;
                }
            }

            .content {
                display: flex;
                justify-content: center;
                padding: 30rpx 0;

                .text {
                    padding-left: 30rpx;
                    color: #ffca3e;
                }
            }

            .footer {
                display: flex;
                justify-content: center;
                align-items: center;
                margin: auto;
                width: 50%;
                padding: 25rpx;


            }
        }
    }
</style>